<?php

namespace app\api\controller;

use app\api\model\Student;

class Base123
{
    public function register()
    {
        $no = input('get.no');
        $name = input('get.name');
        $password = input('get.password');
        if (empty($no) || empty($name) || empty($password)) {
            output_error('请填写完整信息');
        }
        $data = Student::where('member_no', '=', $no)->find();
        if (!empty($data)) output_error('学号已被注册');
        $data = array();
        $data['member_no'] = $no;
        $data['member_name'] = $name;
        $data['member_password'] = md5($password);
        $data['member_token'] = '1';
        $data['member_gender'] = '1';
        $data['add_time'] = time();
        Student::insert($data);
        output_data('1');
    }

    public function findpass()
    {
        $no = input('get.no');
        $name = input('get.name');
        $password = input('get.password');
        if (empty($no) || empty($name) || empty($password)) {
            output_error('请填写完整信息');
        }
        $condition = array();
        $condition[] = ['member_no', '=', $no];
        $condition[] = ['member_name', '=', $name];
        $data = Student::where($condition)->find();
        if (empty($data)) output_error('学号与姓名不匹配');
        Student::where('member_id', '=', $data['member_id'])
            ->update(array('member_password' => md5($password)));
        output_data('1');
    }

    public function login()
    {
        $no = input('get.no');
        $password = input('get.password');
        if (empty($no) || empty($password)) {
            output_error('请填写完整信息');
        }

        $condition = array();
        $condition[] = ['member_no', '=', $no];
        $condition[] = ['member_password', '=', md5($password)];
        $data = Student::where($condition)->find();
        if (empty($data)) output_error('学号与密码不匹配');
        $token = md5($no . time());
        Student::where('member_id', '=', $data['member_id'])
            ->update(array('member_token' => $token));
        output_data($token);
    }

}
